package com.dyb.recuitment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dyb.recuitment.entity.Resume;
import com.dyb.recuitment.entity.ResumeDelivery;
import com.dyb.recuitment.mapper.ResumeDeliveryMapper;
import com.dyb.recuitment.mapper.ResumeMapper;
import com.dyb.recuitment.service.IResumeDeliveryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 简历投递表 服务实现类
 * </p>
 *
 * @author 邓宇斌
 * @since 2022-02-22
 */
@Service
public class ResumeDeliveryServiceImpl extends ServiceImpl<ResumeDeliveryMapper, ResumeDelivery> implements IResumeDeliveryService {

    @Autowired
    private ResumeDeliveryMapper resumeDeliveryMapper;

    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public int add(ResumeDelivery resumeDelivery) {
        LocalDateTime localDateTime = LocalDateTime.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), df);
        resumeDelivery.setCreateTime(localDateTime).setUpdateTime(localDateTime);
        ResumeDelivery exist_resumeDelivery = new ResumeDelivery();
        exist_resumeDelivery.setUserId(resumeDelivery.getUserId()).setJobId(resumeDelivery.getJobId()).setStatus(1);
        List<ResumeDelivery> exist_resumeDeliverys = selectByResumeDelivery(exist_resumeDelivery);
        if (exist_resumeDeliverys.size() <= 0) {
            return resumeDeliveryMapper.insert(resumeDelivery);
        }
        return 0;
    }

    @Override
    public int delete(ResumeDelivery resumeDelivery) {
        LocalDateTime localDateTime = LocalDateTime.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), df);
        resumeDelivery.setStatus(0).setUpdateTime(localDateTime);
        return resumeDeliveryMapper.updateById(resumeDelivery);
    }

    @Override
    public ResumeDelivery getById(ResumeDelivery resumeDelivery) {
        List<ResumeDelivery> resumeDeliveries = selectByResumeDelivery(resumeDelivery);
        if (resumeDeliveries.size() > 0) {
            return resumeDeliveries.get(0);
        }
        return null;
    }

    @Override
    public List<ResumeDelivery> selectByResumeDelivery(ResumeDelivery resumeDelivery) {
        resumeDelivery.setStatus(1);
        QueryWrapper<ResumeDelivery> queryWrapper = new QueryWrapper<>(resumeDelivery);
        return resumeDeliveryMapper.selectList(queryWrapper);
    }

    @Override
    public List<ResumeDelivery> selectAllResumeDelivery() {
        return resumeDeliveryMapper.selectList(new QueryWrapper<>());
    }

}
